package com.jiangjiang.eshop.wms.stock;

import com.jiangjiang.eshop.schedule.dto.SaleDeliveryScheduleResult;
import com.jiangjiang.eshop.schedule.service.ScheduleService;
import com.jiangjiang.eshop.wms.domain.WmsGoodsAllocationStock;
import com.jiangjiang.eshop.wms.domain.WmsGoodsAllocationStockDetail;
import com.jiangjiang.eshop.wms.domain.WmsGoodsStock;
import com.jiangjiang.eshop.wms.dto.ReturnGoodsInputOrderDTO;
import com.jiangjiang.eshop.wms.dto.ReturnGoodsInputOrderItemDTO;
import com.jiangjiang.eshop.wms.service.WmsGoodsAllocationStockDetailService;
import com.jiangjiang.eshop.wms.service.WmsGoodsAllocationStockService;
import com.jiangjiang.eshop.wms.service.WmsGoodsStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @Author Jianghaowen
 * @Date 2021/9/14
 * @Description: TODO
 * @Version V1.0
 **/
@Component
public class ReturnGoodsInputWmsStockUpdater extends AbstractWmsStockUpdater {
    /**
     * 退货入库单
     */
    private ReturnGoodsInputOrderDTO returnGoodsInputOrder;
    /**
     * 商品库存管理的DAO组件
     */
    @Autowired
    private WmsGoodsStockService goodsStockService;
    /**
     * 货位库存管理的DAO组件
     */
    @Autowired
    private WmsGoodsAllocationStockService goodsAllocationStockService;
    /**
     * 货位库存明细管理的DAO组件
     */
    @Autowired
    private WmsGoodsAllocationStockDetailService goodsAllocationStockDetailService;
    /**
     * 调度中心接口
     */
    @Autowired
    private ScheduleService scheduleService;


    @Override
    protected void updateGoodsStock() throws Exception {
        Set<Long> skuIds = returnGoodsInputOrder.getItems()
                .stream()
                .map(ReturnGoodsInputOrderItemDTO::getGoodSkuId)
                .collect(Collectors.toSet());

        Map<Long, WmsGoodsStock> goodsStockMap = goodsStockService.queryBySkuIds(skuIds)
                .stream()
                .collect(Collectors.toMap(WmsGoodsStock::getGoodsSkuId, Function.identity()));

        List<WmsGoodsStock> goodsStocks = returnGoodsInputOrder.getItems().stream().map(item -> {
            WmsGoodsStock wmsGoodsStock = goodsStockMap.get(item.getGoodSkuId());
            wmsGoodsStock.setAvailableStockQuantity(wmsGoodsStock.getAvailableStockQuantity() + item.getArrivalCount());
            wmsGoodsStock.setOutputStockQuantity(wmsGoodsStock.getOutputStockQuantity() - item.getArrivalCount());
            return wmsGoodsStock;
        }).collect(Collectors.toList());

        goodsStockService.updateBatchById(goodsStocks);

    }

    @Override
    protected void updateGoodsAllocationStock() throws Exception {
        for (ReturnGoodsInputOrderItemDTO item : returnGoodsInputOrder.getItems()) {
            Map<Long, WmsGoodsAllocationStock> allocationStockMap = goodsAllocationStockService.queryBySkuId(item.getGoodSkuId())
                    .stream()
                    .collect(Collectors.toMap(WmsGoodsAllocationStock::getGoodsAllocationId, Function.identity()));
            SaleDeliveryScheduleResult scheduleResult = scheduleService.getScheduleResult(returnGoodsInputOrder.getOrderId(), item.getGoodSkuId());
            List<WmsGoodsAllocationStock> stocks = scheduleResult.getPickingItems().stream().map(pickingItem -> {
                WmsGoodsAllocationStock stock = allocationStockMap.get(pickingItem.getGoodsAllocationId());
                stock.setAvailableStockQuantity(stock.getAvailableStockQuantity() + pickingItem.getPickingCount());
                stock.setOutputStockQuantity(stock.getOutputStockQuantity() - pickingItem.getPickingCount());
                return stock;
            }).collect(Collectors.toList());

            goodsAllocationStockService.updateBatchById(stocks);
        }
    }

    @Override
    protected void updateGoodsAllocationStockDetail() throws Exception {
        for (ReturnGoodsInputOrderItemDTO item : returnGoodsInputOrder.getItems()) {
            List<WmsGoodsAllocationStockDetail> stockDetails = item.getPutOnItems().stream().map(putOnItem -> {
                WmsGoodsAllocationStockDetail stockDetail = new WmsGoodsAllocationStockDetail();
                stockDetail.setGoodsAllocationId(putOnItem.getGoodsAllocationId());
                stockDetail.setGoodsSkuId(putOnItem.getGoodsSkuId());
                stockDetail.setPutOnQuantity(putOnItem.getPutOnShelvesCount());
                stockDetail.setPutOnTime(putOnItem.getGmtCreate());
                stockDetail.setCurrentStockQuantity(stockDetail.getPutOnQuantity());
                stockDetail.setLockedStockQuantity(0L);
                return stockDetail;
            }).collect(Collectors.toList());
            goodsAllocationStockDetailService.saveBatch(stockDetails);
            item.setStockDetails(goodsAllocationStockDetailService.convertEntities2DTOList(stockDetails));
        }

    }

    @Override
    public void setParameter(Object parameter) {
        returnGoodsInputOrder = (ReturnGoodsInputOrderDTO) parameter;
    }
}
